// pages/player/player.js
let musiclist = []
let nowPlayinfIndex = 0
// 获取全局背景音乐管理器
const backgroundAudioManager = wx.getBackgroundAudioManager()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUrl: '',
    isPlaying: false,
    isLyricShow: false,
    lyric: '',
    isSame: false   // 是否为同一首歌曲
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    nowPlayinfIndex = options.index
    musiclist = wx.getStorageSync('musiclist');
    this._loadMusicDetail(options.musicId)
  },

  _loadMusicDetail (musicId) {
    if (musicId == app.getPlayingMusicId()) {
      this.setData({
        isSame: true
      })
    } else {
      this.setData({
        isSame: false
      })
    }
    if (!this.data.isSame) {
      backgroundAudioManager.stop()
    }
    let music = musiclist[nowPlayinfIndex]
    wx.setNavigationBarTitle({
      title: music.name,
    });
    this.setData({
      picUrl: music.al.picUrl,
      isPlaying: false
    })
    app.setPlayingMusicId(musicId)
    wx.showLoading({ title: '歌曲加载中' })
    wx.cloud.callFunction({
      name: 'music',
      data: {
        musicId,
        $url: 'musicUrl'
      }
    }).then(res => {
      if (res.result.data[0].url == null) {
        wx.showToast({
          title: '无权限播放'
        })
        return
      }
      if (!this.data.isSame) {
        backgroundAudioManager.src = res.result.data[0].url
        backgroundAudioManager.title = music.name
        backgroundAudioManager.coverImgUrl = music.al.picUrl
        backgroundAudioManager.singer = music.ar[0].name
        backgroundAudioManager.epname = music.al.name
        // 保存播放历史
        this.savePlayHistory()
      }
      this.setData({
        isPlaying: true
      })
      wx.hideLoading()
      // 加载歌词
      wx.cloud.callFunction({
        name: 'music',
        data: {
          musicId,
          $url: 'lyric'
        }
      }).then(res => {
        let lyric = '暂无歌词'
        const lrc = res.result.lrc
        if (lrc) {
          lyric = lrc.lyric
        }
        this.setData({
          lyric
        })
      })
    })
  },
  // 播放或者暂停
  togglePlaying () {
    if (this.data.isPlaying) {
      backgroundAudioManager.pause()
    } else {
      backgroundAudioManager.play()
    }
    this.setData({
      isPlaying: !this.data.isPlaying
    })
  },
  onPrev () {
    nowPlayinfIndex--
    if (nowPlayinfIndex < 0) {
      nowPlayinfIndex = musiclist.length - 1
    }
    this._loadMusicDetail(musiclist[nowPlayinfIndex].id)
  },
  onNext () {
    nowPlayinfIndex++
    if (nowPlayinfIndex === musiclist.length) {
      nowPlayinfIndex = 0
    }
    this._loadMusicDetail(musiclist[nowPlayinfIndex].id)
  },
  onChangeLyricShow () {
    this.setData({
      isLyricShow: !this.data.isLyricShow
    })
  },

  timeUpdate (event) {
    this.selectComponent('.lyric').updata(event.detail.currentTime)
  },

  onPlay () {
    this.setData({
      isPlaying: true
    })
  },
  onPause () {
    this.setData({
      isPlaying: false
    })
  },
  // 保存播放历史
  savePlayHistory () {
    const playingMusic = musiclist[nowPlayinfIndex]
    const openid = app.globalData.openid
    const history = wx.getStorageSync(openid)
    let isHave = false
    for (let i = 0; i < history.length; i++) {
      if (history[i].id == playingMusic.id) {
        isHave = true
        break
      }
    }
    if (!isHave) {
      history.unshift(playingMusic)
      wx.setStorageSync(openid, history)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})